<?php
//kiem tra gio hang co qua tang hoac zing xu ko?
$checkZingxu = FALSE;
$checkQuatang = FALSE;
foreach ($ObjectCard as $obc) {
    $item_id = $obc['object_id'];
    if (intval($item_id) == 1)
        $checkZingxu = 1; //vinh note: id of zingzu = 1
    $critera = new CDbCriteria();
    $critera->join = 'left join {{object_term}} obt on(obt.`term_id` = t.`term_id`)';
    $critera->condition = 'obt.`object_id`=:id';
    $critera->params = array(':id' => $item_id);
    $item = Term::model()->find($critera);
    if ($item->taxonomy_id != 4)
        $checkQuatang = TRUE; //vinh note: taxonomy the game = 4 
}
//kiem tra gio hang co qua tang hoac zing xu EOF
?>
<div id="main" class="content_main">
    <div class="clear"></div>
    <div class="breadcrumb">
        <ul>
            <li><a href="<?php echo SITE_PATH_URL; ?>">Home</a></li>
            <li><i class="next_cate"></i>
                <?php echo CHtml::link('Shopping card', array('page/render', 'slug' => 'card')); ?>
            </li>
        </ul>
    </div>
    <div class="clear"></div>
    <div class="main_list" >
        <div class="menu">
            <div class="left"><h3>Shopping cart</h3></div>
        </div>

        <div class="main_content" id="id_main_content">
            <?php
            if (count($ObjectCard) > 0) {
                ?>
                <?php
                $form = $this->beginWidget('CActiveForm', array(
                    'id' => 'submit-card-form',
                    'enableAjaxValidation' => false,
                        /* 'htmlOptions'=>array(
                          'onsubmit'=>false,
                          //'onkeypress'=>" if(event.keyCode == 13){ send(); } "
                          ), */
                ));
                ?>
                <div class="listproduct" id="id_list_product_card">
                    <table class="shopping_card">
                        <thead>
                            <tr>
                                <th>Remove</th>
                                <th >Image</th>
                                <th style="text-align: left; padding-left: 10px;">Product name</th>
                                <th>Qty</th>
                                <th>Tatol</th>
                            </tr>
                        </thead>
                        <tbody>

                            <?php
                            foreach ($ObjectCard as $key => $ob) {
                                ?>
                                <tr id="id_item_card_<?php echo $ob['object_id']; ?>">
                                    <td class="remove">
                                        <input type="checkbox" onchange="removeItemCard(<?php echo $ob['object_id']; ?>, this)" class="form-checkbox" value="1" id="edit-items-remove-<?php echo $ob['object_id']; ?>" name="items[<?php echo $ob['object_id']; ?>][remove]">
                                    </td>
                                    <td class="image">
                                        <img width="100" height="60" class="imagecache imagecache-cart" title="" alt="" src="<?php echo $ob['resource_path']; ?>" >
                                    </td>
                                    <td class="product">
                                        <?php
                                        echo CHtml::link($ob['object_name'], array('page/detailcontent', 'term_slug' => $ob['term_slug'], 'id' => $ob['object_id'], 'pslug' => $ob['object_slug']), array('title' => $ob['object_name']));
                                        ?>
                                    </td>
                                    <td class="quantity">
                                        <input type="text" class="form-text" value="<?php echo $ob['numberitem'] ?>" size="5" id="edit-items-0-qty" name="items[<?php echo $ob['object_id']; ?>][qty]" maxlength="6">
                                    </td>
                                    <td class="total" ><?php echo formatprice($ob['sumprice']) . ' ' . GetNotation() ?></td>

                                </tr>
                                <?php
                            }
                            ?>
                            <tr>
                                <td colspan="4" class="subtotal"  >Subtotal:</td>
                                <td class="total" id="total_price_view_card"><?php echo formatprice($subtotal) . ' ' . GetNotation(); ?></td>
                            </tr>               
                        </tbody>
                    </table> 
                    <div class="clear"></div>
                    <div id="wrapper"></div>

                    <!-- Vinh add phan xử lý checkout tren view cart -->
                    <div id="checkout-form" style="display: none;">
                        <div class="form">
                            <?php
                            //echo 'vinh show: '.$checkQuatang.'-'.$checkZingxu;
                            $model = new CheckoutForm();
                            $frmCheckout = ModuleCheckout::model()->findAll('t.`module_status`=:stt order by t.`module_order` ASC', array(':stt' => 1));
                            foreach ($frmCheckout as $frm) {

                                switch ($frm['module_form_name']) {
                                    case "form_zing_id":
                                        if ($checkZingxu) {
                                            $this->render('common.blocks.view_card.form.form_zing_id', array('model' => $model, 'form' => $form));
                                        }
                                        break;
                                    case 'form_billing_information':
                                        $this->render('common.blocks.view_card.form.form_billing_information', array('model' => $model, 'form' => $form));
                                        break;
                                    case 'form_payment_method':
                                        $this->render('common.blocks.view_card.form.form_payment_method', array('model' => $model, 'form' => $form));
                                        break;
                                    case 'form_delivery_form':
                                        //if($checkobjectpresent)
                                        //$this->render('common.blocks.form_checkout.form_delivery_form',array('model'=>$model,'form'=>$form));
                                        break;
                                    case 'form_delivery_method_form':
                                        //if($checkobjectpresent)
                                        //$this->render('common.blocks.form_checkout.form_delivery_method_form',array('model'=>$model,'form'=>$form));
                                        break;
                                }
                                /*
                                  $display ='';
                                  //11: form delivery information
                                  //12: form delivery method
                                  if(!$checkQuatang && (($frm['module_id']==11) || ($frm['module_id']==12) )){
                                  $display = 'style="display:none"';
                                  }
                                  //6: form zing id
                                  if(!$checkZingxu && ($frm['module_id']==6)) $display = 'style="display:none"';
                                  ?>
                                  <fieldset <?=$display?>>
                                  <legend><?=$frm['module_name']?></legend>
                                  <div class="form_fieldset">
                                  <p>Content of form</p>
                                  </div>
                                  </fieldset>
                                  <?php
                                 * 
                                 */
                                //echo '<div id="'.$frm['module_form_name'].'" '.$display.'>'.$frm['module_name'].'</div>';
                            }
                            ?>
                        </div>
                    </div>
                    <!-- Vinh add phan xử lý checkout tren view cart EOF-->

                    <div id="cart-form-buttons">
                        <div id="continue-shopping-link" class="form-submit bnt"><a href="<?php echo SITE_PATH_URL; ?>">Continue shopping</a></div>
                        <input type="submit2" class="form-submit bnt" value="Fast Checkout (Testing)" id="fast-checkout" name="submitcard2">
                        <input type="submit" class="form-submit bnt" value="Checkout" id="edit-checkout" name="submitcard">
                    </div>
                </div>
                <?php $this->endWidget(); ?> 
                <?php
            } else {
                ?>
                <div id="id_error_card_empty" style=" border: none !important;">
                    <p style="color: red;">There are no products in your shopping cart.</p>
                    <div id="continue-shopping-link"><a href="<?php echo SITE_PATH_URL; ?>">Continue shopping</a></div>
                </div>
            <?php }
            ?>

        </div>
    </div>
</div>


<script>
    function removeItemCard(object_id, current) {
        if ($(current).attr('checked')) {
            // 
            $('#edit-checkout').attr('disabled', 'disabled');
            $(current).parents('td').addClass('remove_waiting');
<?php
echo Chtml::ajax(array(
    'url' => Yii::app()->createUrl('objectorder/removeobjectcard'), //;SITE_PATH.'order/revieworder',//array('site/test'),
    'data' => array('object_id' => 'js:object_id'),
    'type' => 'get',
    'success' => "function(str){
                             $(current).parents('td').removeClass('remove_waiting');
                            removesucces(str,object_id);
                            }"
));
?>
        }

    }
    function removesucces(str, object_id)
    {
        $('#edit-checkout').removeAttr("disabled");
        if (str != 'false')
        {
            str = eval('(' + str + ')');
            $('#id_item_card_' + object_id).remove();
            $('#item_gio_hang').text(str['card'][0]['item'] + ' (item)');
            $('#total_price_gio_hang').text(str['card'][0]['total_price'] + ' ' + '<?php echo GetNotation(); ?>');
            $('#total_price_view_card').text(str['card'][0]['total_price'] + ' ' + '<?php echo GetNotation(); ?>');
            //  alert(str['card'][0]['item']+':'+str['card'][0]['total_price']);
            if (str['card'][0]['item'] == 0)
            {
                $html = '<div id="id_error_card_empty" style=" border: none !important; text-align: center">';
                $html += '<p style="color: red; font-size: 20px;">There are no products in your shopping cart.</p>';
                $html += '<div id="continue-shopping-link" style="float: none; clear: both;"><a href="<?php echo SITE_PATH_URL; ?>">Continue shopping</a></div>';
                $html += '</div>';
                $('#id_list_product_card').remove();
                $('#id_main_content').append($html);


            }
        }
        else {
        }
        // $('#item_gio_hang').text('thanh');
        //  total_price_gio_hang

    }

    $(document).ready(function() {
        $('#fast-checkout').click(function() {
            var toLoad = $('#checkout-form');
            //$('#id_main_content').remove();
            $('.load_view_payment').remove();
            $('#wrapper').append('<div class="load_view_payment"></div>');
            $('.load_view_payment').fadeIn('normal');
            loadContent();
            function loadContent() {
                $('#checkout-form').load(toLoad, '', showNewContent())
            }
            function showNewContent() {
                $('#checkout-form').show('normal', hideLoader());
            }
            function hideLoader() {
                $('.load_view_payment').fadeOut('normal');
            }
        });
        return false;
    });
</script>